<?php

use Drupal\ui_mlt\Entity\UiMlt;

function ui_mlt_add_form() {
  $entity = new UiMlt();
  module_load_include('inc', 'ui_mlt', 'ui_mlt.pages');
  return drupal_get_form('ui_mlt_form', $entity, 'add');
}

function ui_mlt_edit_form($entity) {
  module_load_include('inc', 'ui_mlt', 'ui_mlt.pages');
  return drupal_get_form('ui_mlt_form', $entity, 'edit');
}

function ui_mlt_form($form, &$form_state, UiMlt $entity, $op) {


  $form_state['ui_mlt'] = $entity;

  $langcode = entity_language('ui_mlt', $entity);
  if (empty($langcode)) {
    $langcode = LANGUAGE_NONE;
  }


  $form['path'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    'item_title' => [
      '#type' => 'textfield',
      '#title' => t('标题'),
      '#description' => t('如果链接的路径标题为空，则使用这个标题。'),
      '#default_value' => $entity->item_title,
      '#size' => 60,
      '#maxlength' => 128,
      '#required' => TRUE,
    ],
    'root_path' => [
      '#type' => 'textfield',
      '#title' => t('路径'),
      '#default_value' => $entity->root_path,
      '#size' => 60,
      '#maxlength' => 128,
      '#required' => TRUE,
      '#element_validate' => ['ui_mlt_element_validate_menu'],
    ],
    'item' => [
      '#type' => 'textfield',
      '#title' => t('按钮链接'),
      '#default_value' => $entity->item,
      '#size' => 60,
      '#maxlength' => 128,
      '#required' => TRUE,
      '#element_validate' => ['ui_mlt_element_validate_menu'],
    ],
    'destination' => [
      '#type' => 'checkbox',
      '#default_value' => $entity->destination,
      '#title' => t('是否回跳.'),
    ],

    'query' => [
      '#type' => 'textarea',
      '#default_value' => $entity->query,
      '#title' => t('Query 数据.json格式'),
    ],

  ];

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
    '#weight' => 40,
  ];
  if (!empty($entity->id)) {
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => t('删除'),
      '#weight' => 45,
      '#limit_validation_errors' => [],
      '#submit' => ['ui_mlt_form_submit_delete'],
    ];
  }
  return $form;
}

function ui_mlt_element_validate_menu($element, &$form_state, $form) {
  $value = $element['#value'];

  global $base_url;
  $url = $base_url . '/';
  $result = substr_compare($value, $url, 0, strlen($url), TRUE);
  if ($result === 0) {
    $value = substr($value, strlen($url));
  }

  if (!empty(strstr($value, "?"))) {
    list($path, $args) = explode('?', $value);
  }
  else {
    $path = $value;
  }

  if ($element['#required'] && !empty($value) && empty(menu_get_item($path))) {
    form_error($element, t('路径无效.'));
  }
}

function ui_mlt_form_submit($form, &$form_state) {
  $entity = &$form_state['ui_mlt'];

  $item_title = $form_state['values']['path']['item_title'];
  $root_path = $form_state['values']['path']['root_path'];
  $destination = $form_state['values']['path']['destination'];
  $item = $form_state['values']['path']['item'];
  if ($entity instanceof UiMlt) {
    $entity->item_title = $item_title;

    global $base_url;
    $url = $base_url . '/';

    $result = substr_compare($root_path, $url, 0, strlen($url), TRUE);
    if ($result === 0) {
      $root_path = substr($root_path, strlen($url));
    }

    $result = substr_compare($item, $url, 0, strlen($url), TRUE);
    if ($result === 0) {
      $item = substr($item, strlen($url));
    }

    $entity->root_path = $root_path;
    $entity->item = $item;


    $entity->destination = $destination;

    $entity->query = $form_state['values']['path']['query'];

    $entity->save();
  }
}

function ui_mlt_form_submit_delete($form, &$form_state) {
  $entity = &$form_state['ui_mlt'];
  $form_state['redirect'] = 'ui-mlt/' . $entity->id . '/delete';
}

function ui_mlt_delete_confirm($form, &$form_state, UiMlt $entity) {
  $form['#ui_mlt'] = $entity;
  $form['id'] = ['#type' => 'value', '#value' => $entity->id];
  return confirm_form($form,
    //消息
    t('您确定要删除这个消息:ID:%id', [
      '%id' => $entity->id,
    ]),
    //点取消返回地址
    'ui-mlt/' . $entity->id . '/edit',
    //提示
    t('这个操作不能恢复.'),
    //按钮1
    t('删除'),
    //按钮2
    t('取消')
  );
}

function ui_mlt_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    $id = $form_state['values']['id'];
    entity_delete('ui_mlt', $id);
  }
  $form_state['redirect'] = 'admin/content/mlt';
}
